// mircoApp/cloud_shop/brandSales/brandSales.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sort_type: 1,
    is_background: "",
    page: 1,
    total_page: 0,
    isLoadMore: true,
    dataList: [],
    self_reward_name: "",
    isName:null,
    brand_follow_status:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.name){
      this.setData({
        isName : options.name
      })
      wx.setNavigationBarTitle({
        title: '畅销品牌榜',
      })
    }
    let language = wx.getStorageSync("langIndex");
    let basic_info = wx.getStorageSync('yz_basic_info').plugin_setting.cloud_shop;
    if(basic_info){
      this.setData({
        language: language.en,
        self_reward_name: basic_info.self_reward_name,
        sort_type: 1,
        brand_follow_status: basic_info.brand_follow_status
      })
    }
    
    this.getData();
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      dataList: []
    })
  },
  toGood(e){
    let {item} = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id='+item.id,
    })
  },
  sortTap(e) {
    let {
      key
    } = e.currentTarget.dataset;
    this.setData({
      sort_type: key,
    })
    this.getData();
  },
  async tapFollowM(e){
    let {item,index} = e.currentTarget.dataset;
    let json = {
      brand_id: item.id,
      status: item.is_follow ? 0 : 1,
    };
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.cloud-shop.frontend.brand.follow"),
      data: json,
    });
    if (data.result) {
      this.setData({
        ['dataList['+index+'].is_follow']: item.is_follow ? 0 : 1,
      });
      app.tips(data.msg);
    } else {
      app.tips(data.msg);
    }
  },
  async getData() {
    let urlStr = "plugin.cloud-shop.frontend.self-sales.index";
    let json = {};
    if (this.data.isName == "new") {
      urlStr = "plugin.cloud-shop.frontend.many-sales.index";
    } else {
      json = { sort_type: this.data.sort_type };
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlStr),
      data: json
    })
    if (data.result) {
      this.setData({
        is_background: data.data.background,
        dataList: data.data.list.data,
        total_page: data.data.list.last_page,
        isLoadMore: true
      })
    } else {
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1,
      })
      let json = {
        sort_type: this.data.active,
        page: this.data.page
      };
      let urlStr = "plugin.cloud-shop.frontend.self-sales.index";
      if (this.data.isName == "new") {
        urlStr = "plugin.cloud-shop.frontend.many-sales.index";
        json = { page: this.data.page };
      } else {
        json = { sort_type: this.data.active,page: this.data.page };
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(urlStr),
        data: json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          dataList: this.data.dataList.concat(data.data.list.data)
        })
      } else {
        this.setData({
          page: this.data.page - 1
        })
        app.tips(data.msg)
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})